package com.love.cloud.common.mybatis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.cloud.common.mybatis.entity.BaseFileEntity;
import com.love.cloud.common.mybatis.entity.FileBean;
import com.love.cloud.common.mybatis.entity.SimpleBaseEntity;
import com.love.cloud.common.mybatis.mapper.FileBeanMapper;
import com.love.cloud.common.mybatis.service.FileBeanService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author hongdongqu
 */
@Service
public class FileBeanServiceImpl extends ServiceImpl<FileBeanMapper, FileBean> implements FileBeanService {

    @Override
    public List<FileBean> getFileBeansByRelationInfo(Long relationId, String relationType) {
        QueryWrapper<FileBean> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(FileBean::getRelationId,relationId)
                .eq(FileBean::getRelationType,relationType);
        List<FileBean> list = list(queryWrapper);
        return list!=null?list:new ArrayList<FileBean>();
    }

    @Override
    public List<FileBean> getFileBeansByRelationInfo(Set<Long> relationId, String relationType) {
        QueryWrapper<FileBean> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().in(FileBean::getRelationId,relationId)
                .eq(FileBean::getRelationType,relationType);
        List<FileBean> list = list(queryWrapper);
        return list!=null?list:new ArrayList<FileBean>();
    }

    @Override
    public void setBatchFileBeansByRelationInfo(String relationType, List listTmp) {
        List<SimpleBaseEntity> list=(List<SimpleBaseEntity>) listTmp;
        //通过关联类型和关联id来获取到文件信息
        Set<Long> ids = list.stream().map(SimpleBaseEntity::getId).collect(Collectors.toSet());
        this.setBatchFileBeansByRelationInfo(relationType,listTmp,ids);
    }

    @Override
    public void setBatchFileBeansByRelationInfo(String relationType, List list, Set<Long> ids) {
        if(ids!=null&&ids.size()>0) {
            QueryWrapper<FileBean> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().in(FileBean::getRelationId, ids)
                    .eq(FileBean::getRelationType, relationType);
            List<FileBean> fileBeanList = list(queryWrapper);
            //将文件信息分组
            Map<Long, List<FileBean>> groupFileBeanList = fileBeanList.stream().collect(Collectors.groupingBy(FileBean::getRelationId));
            for (Object object : list) {
                if (object instanceof BaseFileEntity) {
                    ((BaseFileEntity) object).setFileBeanList(groupFileBeanList.get(((BaseFileEntity) object).getId()));
                } else {
                    //todo 根据其中的类方法来直接set
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<FileBean> saveFileBean(List<FileBean> fileBeanList, Long relationId, String relationType) {
        if(fileBeanList!=null){
            for (FileBean fileBean :fileBeanList) {
                fileBean.setId(null);
                fileBean.setRelationId(relationId);
                fileBean.setRelationType(relationType);
                this.save(fileBean);
            }
        }
        return fileBeanList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<FileBean> updateFileBean(List<FileBean> fileBeanList, Long relationId, String relationType) {
        List<FileBean> oldFileBeanList = this.getFileBeansByRelationInfo(relationId, relationType);
        this.updateFileBean(fileBeanList,oldFileBeanList,relationId,relationType);
        return fileBeanList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<FileBean> updateFileBean(List<FileBean> fileBeanList, List<FileBean> oldFileBeanList, Long relationId, String relationType) {
        Set<Long> collect = fileBeanList.stream().map(FileBean::getId).collect(Collectors.toSet());
        oldFileBeanList.forEach(v -> {
            if (v.getId()!=null&&!collect.contains(v.getId())) {
                this.removeById(v);
            }
        });
        fileBeanList.forEach(v -> {
            v.setRelationId(relationId);
            v.setRelationType(relationType);
        });
        this.saveOrUpdateBatch(fileBeanList);
        return fileBeanList;
    }
}
